﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RPSImporter.Models;
using System.Web.Security;
using RPSImporter.Services;

namespace RPSImporter.Controllers
{
    public class LogInController : Controller
    {
        //
        // GET: /LogIn/

        public ActionResult LogIn()
        {
            LogInModel model = new LogInModel();
            model.LogInError = string.Empty;
            return View(model);
        }

        [HttpPost]
        public ActionResult LogIn(LogInModel model)
        {
            if (ModelState.IsValid)
            {
               if (DatabaseSvc.Instance.Settings.Users.Any(u => (u.UserName.ToLower() == model.UserName.ToLower()) && (u.UserPassword == model.UserPass)))
                {
                   Session["currentUser"] = DatabaseSvc.Instance.Settings.Users.Where(u => (u.UserName.ToLower() == model.UserName.ToLower()) && (u.UserPassword == model.UserPass)).FirstOrDefault();
                    FormsAuthentication.SetAuthCookie(model.UserName, false);
                    model.LogInError = string.Empty;
                    return RedirectToAction("Index", "Home");
                }
            }

            model.LogInError = "Utilisateur ou Mot de Passe Non Valide!";

            return View(model);
        }

        public ActionResult FtpLogIn(string UserName, string UserPass)
        {
            if (!String.IsNullOrEmpty(UserName) && !String.IsNullOrEmpty(UserPass))
            {
               Session["currentUser"] = DatabaseSvc.Instance.Settings.Users.Where(u => (u.UserName.ToLower() == UserName.ToLower()) && (u.UserPassword == UserPass)).FirstOrDefault();
                FormsAuthentication.SetAuthCookie(UserName, false);
                return RedirectToAction("ImportFtp", "Home");
            }

            return View();
        }
    }
}
